package cn.iocoder.yudao.module.liveprice.kafka;

import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.common.serialization.StringDeserializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.annotation.EnableKafka;
import org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory;
import org.springframework.kafka.core.ConsumerFactory;
import org.springframework.kafka.core.DefaultKafkaConsumerFactory;
import org.springframework.kafka.support.serializer.ErrorHandlingDeserializer;
import org.springframework.kafka.support.serializer.JsonDeserializer;

import java.util.HashMap;
import java.util.Map;

@EnableKafka
@Configuration
public class KafkaConsumerConfig {

    @Bean
    public JsonDeserializer<NowApiPriceMessage> nowApiJsonDeserializer() {
        return new JsonDeserializer<>(NowApiPriceMessage.class);
    }

    @Bean
    public JsonDeserializer<RtjDataPriceMessage> rtjDataJsonDeserializer() {
        return new JsonDeserializer<>(RtjDataPriceMessage.class);
    }

    @Bean
    public ConcurrentKafkaListenerContainerFactory<String, NowApiPriceMessage> nowApiKafkaListenerContainerFactory() {
        ConcurrentKafkaListenerContainerFactory<String, NowApiPriceMessage> factory = new ConcurrentKafkaListenerContainerFactory<>();
        factory.setConsumerFactory(nowApiConsumerFactory());
        return factory;
    }

    @Bean
    public ConcurrentKafkaListenerContainerFactory<String, RtjDataPriceMessage> rtjDataKafkaListenerContainerFactory() {
        ConcurrentKafkaListenerContainerFactory<String, RtjDataPriceMessage> factory = new ConcurrentKafkaListenerContainerFactory<>();
        factory.setConsumerFactory(rtjDataConsumerFactory());
        return factory;
    }

    @Bean
    public ConsumerFactory<String, NowApiPriceMessage> nowApiConsumerFactory() {
        Map<String, Object> props = new HashMap<>();
        props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "118.89.73.223:9092");
        props.put(ConsumerConfig.GROUP_ID_CONFIG, "LIVE_PRICE_CONSUMER");
        props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
        props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, ErrorHandlingDeserializer.class);
        props.put("spring.deserializer.value.delegate.class", JsonDeserializer.class);
        props.put("spring.json.trusted.packages", "*");

        return new DefaultKafkaConsumerFactory<>(props, new StringDeserializer(), nowApiJsonDeserializer());
    }

    @Bean
    public ConsumerFactory<String, RtjDataPriceMessage> rtjDataConsumerFactory() {
        Map<String, Object> props = new HashMap<>();
        props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "118.89.73.223:9092");
        props.put(ConsumerConfig.GROUP_ID_CONFIG, "LIVE_PRICE_CONSUMER");
        props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
        props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, ErrorHandlingDeserializer.class);
        props.put("spring.deserializer.value.delegate.class", JsonDeserializer.class);
        props.put("spring.json.trusted.packages", "*");

        return new DefaultKafkaConsumerFactory<>(props, new StringDeserializer(), rtjDataJsonDeserializer());
    }

}